From 1fcdd9f7d859b925bf92265f441655d5522e351c Mon Sep 17 00:00:00 2001 From: Factiven Date: Tue, 11 Apr 2023 23:23:29 +0700 Subject: initial commit --- pages/manga/chapter/[chapter].js | 423 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 423 insertions(+) create mode 100644 pages/manga/chapter/[chapter].js (limited to 'pages/manga/chapter/[chapter].js') diff --git a/pages/manga/chapter/[chapter].js b/pages/manga/chapter/[chapter].js new file mode 100644 index 0000000..8d2a5a1 --- /dev/null +++ b/pages/manga/chapter/[chapter].js @@ -0,0 +1,423 @@ +import axios from "axios"; +import Head from "next/head"; +import { useEffect, useState } from "react"; +import Navbar from "../../../components/navbar"; +import Footer from "../../../components/footer"; +import ScrollTracker from "../../../components/scrollTracker"; + +export default function Test({ title, id, aniId, data, provider }) { + const [isLoading, setIsLoading] = useState(true); + const [datas, setData] = useState(data); + const [currentChapter, setCurrentChapter] = useState(null); + const [nextChapter, setNextChapter] = useState(null); + const [prevChapter, setPrevChapter] = useState(null); + + useEffect(() => { + function storedData() { + const nowChap = localStorage.getItem("currentChapterId"); + const chapt = localStorage.getItem("chapters"); + const chapters = JSON.parse(chapt); + const currentChapter = chapters.find((chapter) => chapter.id === nowChap); + const currentIndex = chapters.findIndex( + (chapter) => chapter.id === nowChap + ); + + const nextIndex = currentIndex + 1; + + const nextChapter = chapters[nextIndex]; + const prevIndex = currentIndex - 1; + + setNextChapter(nextChapter); + setCurrentChapter(currentChapter); + setPrevChapter(chapters[prevIndex]); + setIsLoading(false); + } + storedData(); + }, []); + + function getNextChapter() { + window.scrollTo(0, 0); + setIsLoading(true); + const currentId = localStorage.getItem("currentChapterId"); + const scrollData = JSON.parse(localStorage.getItem("watchedManga")) || []; + const scroll = localStorage.getItem("scrollPercentage"); + if (scroll >= 5) { + const existingDataIndex = scrollData.findIndex( + (data) => data.id === currentId + ); + if (existingDataIndex !== -1) { + // Update existing data + scrollData[existingDataIndex].timestamp = Date.now(); + scrollData[existingDataIndex].percentage = parseFloat( + localStorage.getItem("scrollPercentage") + ); + } else { + // Add new data + scrollData.push({ + timestamp: Date.now(), + percentage: parseFloat(localStorage.getItem("scrollPercentage")), + id: currentId, + }); + } + + localStorage.setItem("watchedManga", JSON.stringify(scrollData)); + + const chapt = localStorage.getItem("chapters"); + const chapters = JSON.parse(chapt); + + const currentIndex = chapters.findIndex( + (chapter) => chapter.id === currentId + ); + + const nextIndex = currentIndex + 1; + + const nextChapter = chapters[nextIndex]; + const nexttChapter = chapters[nextIndex + 1]; + const prevChapter = chapters[nextIndex - 1]; + + setNextChapter(nexttChapter); + setCurrentChapter(nextChapter); + setPrevChapter(prevChapter); + + if (!nextChapter) { + return; + } + + fetch( + `https://api.moopa.my.id/meta/anilist-manga/read?chapterId=${nextChapter.id}&provider=${provider}` + ) + .then((response) => response.json()) + .then((data) => { + if (provider === "mangakakalot") { + const datas = data.map((item) => ({ + img: `https://api.consumet.org/utils/image-proxy?url=${item.img}&referer={Referer:'https://mangakakalot.com/'}`, + page: item.page, + title: item.title, + })); + // console.log(datas); + setData(datas); + } else { + setData(data); + } + }) + .finally(() => { + setIsLoading(false); + }); + + // Update the current chapter id in local storage + localStorage.setItem("currentChapterId", nextChapter.id); + } + + const chapt = localStorage.getItem("chapters"); + const chapters = JSON.parse(chapt); + + const currentIndex = chapters.findIndex( + (chapter) => chapter.id === currentId + ); + + const nextIndex = currentIndex + 1; + + const nextChapter = chapters[nextIndex]; + const nexttChapter = chapters[nextIndex + 1]; + const prevChapter = chapters[nextIndex - 1]; + + setNextChapter(nexttChapter); + setCurrentChapter(nextChapter); + setPrevChapter(prevChapter); + + if (!nextChapter) { + return; + } + + fetch( + `https://api.moopa.my.id/meta/anilist-manga/read?chapterId=${nextChapter.id}&provider=${provider}` + ) + .then((response) => response.json()) + .then((data) => { + if (provider === "mangakakalot") { + const datas = data.map((item) => ({ + img: `https://api.consumet.org/utils/image-proxy?url=${item.img}&referer={Referer:'https://mangakakalot.com/'}`, + page: item.page, + title: item.title, + })); + // console.log(datas); + setData(datas); + } else { + setData(data); + } + }) + .finally(() => { + setIsLoading(false); + }); + + // Update the current chapter id in local storage + localStorage.setItem("currentChapterId", nextChapter.id); + } + // console.log(data); + + function getPrevChapter() { + // Get the current id + window.scrollTo(0, 0); + setIsLoading(true); + + const currentId = localStorage.getItem("currentChapterId"); + const scrollData = JSON.parse(localStorage.getItem("watchedManga")) || []; + const scroll = localStorage.getItem("scrollPercentage"); + if (scroll >= 5) { + const existingDataIndex = scrollData.findIndex( + (data) => data.id === currentId + ); + if (existingDataIndex !== -1) { + // Update existing data + scrollData[existingDataIndex].timestamp = Date.now(); + scrollData[existingDataIndex].percentage = parseFloat( + localStorage.getItem("scrollPercentage") + ); + } else { + // Add new data + scrollData.push({ + timestamp: Date.now(), + percentage: parseFloat(localStorage.getItem("scrollPercentage")), + id: currentId, + }); + } + + localStorage.setItem("watchedManga", JSON.stringify(scrollData)); + + const chapt = localStorage.getItem("chapters"); + const chapters = JSON.parse(chapt); + + const currentIndex = chapters.findIndex( + (chapter) => chapter.id === currentId + ); + + const nextIndex = currentIndex + 1; + + const nextChapter = chapters[nextIndex]; + const nexttChapter = chapters[nextIndex + 1]; + const prevChapter = chapters[nextIndex - 1]; + + setNextChapter(nexttChapter); + setCurrentChapter(nextChapter); + setPrevChapter(prevChapter); + + if (!nextChapter) { + return; + } + + fetch( + `https://api.moopa.my.id/meta/anilist-manga/read?chapterId=${nextChapter.id}&provider=${provider}` + ) + .then((response) => response.json()) + .then((data) => { + if (provider === "mangakakalot") { + const datas = data.map((item) => ({ + img: `https://api.consumet.org/utils/image-proxy?url=${item.img}&referer={Referer:'https://mangakakalot.com/'}`, + page: item.page, + title: item.title, + })); + // console.log(datas); + setData(datas); + } else { + setData(data); + } + }) + .finally(() => { + setIsLoading(false); + }); + + // Update the current chapter id in local storage + localStorage.setItem("currentChapterId", nextChapter.id); + } + + const chapt = localStorage.getItem("chapters"); + const chapters = JSON.parse(chapt); + + const currentIndex = chapters.findIndex( + (chapter) => chapter.id === currentId + ); + + const prevIndex = currentIndex - 1; + + const prevChapter = chapters[prevIndex]; + const nextChapter = chapters[prevIndex + 1]; + const prevvChapter = chapters[prevIndex - 1]; + setCurrentChapter(prevChapter); + setNextChapter(nextChapter); + setPrevChapter(prevvChapter); + + fetch( + `https://api.moopa.my.id/meta/anilist-manga/read?chapterId=${prevChapter.id}&provider=${provider}` + ) + .then((response) => response.json()) + .then((data) => { + // console.log(data); + if (provider === "mangakakalot") { + const datas = data.map((item) => ({ + img: `https://api.consumet.org/utils/image-proxy?url=${item.img}&referer={Referer:'https://mangakakalot.com/'}`, + page: item.page, + title: item.title, + })); + // console.log(datas); + setData(datas); + } else { + setData(data); + } + }) + .finally(() => { + setIsLoading(false); + }); + + // Update the current chapter id in local storage + localStorage.setItem("currentChapterId", prevChapter.id); + } + // console.log({ PREV_CHAPTER: prevChapter }); + // console.log({ CURRENT_CHAPTER: currentChapter }); + // console.log({ NEXT_CHAPTER: nextChapter }); + + return ( + <> + + {title} + + + + + + + +
+
+ {isLoading ? ( +
+ ) : currentChapter && currentChapter.chapter ? ( +

Chapter {currentChapter.chapter}

+ ) : ( +

{currentChapter.title}

+ )} +
+ + {isLoading ? ( +

Loading...

+ ) : ( +
+ {datas.length > 0 && + datas.map((item, index) => ( + {`Page + ))} +
+ )} + {isLoading ? ( +
+ ) : ( + <> +
+
+ + + +
+
+ + + +
+
+ +
+ {nextChapter ? ( + nextChapter.chapter ? ( +

Next Chapter {nextChapter.chapter}

+ ) : ( +

Next Chapter {nextChapter.title}

+ ) + ) : ( +

End of Chapter

+ )} +
+ + )} +
+